<template>
  <div style="display: inline">
    <el-button @click="use1688" type="1688" :disabled="this.orderArr.length === 0" class="mgr10 mgb10">传1688订单</el-button>
    <el-dialog :title="`填写1688订单号`" :visible.sync="dialogForm">
      <template slot="title">
        <div class="header-title">
          <span>填写1688订单号</span>
          <span style="font-size: 14px">(当前选中订单数量：{{this.orderArr.length}})</span>
        </div>

      </template>
      <div class="order1688-tips">
        填入1688订单号，系统将为您自动获取订单的物流状态
      </div>
      <el-form :model="currentForm" label-width="150px" :rules="rules" ref="currentForm">
        <el-form-item label="订单号" prop="order1688">
          <el-input
            v-model.trim="currentForm.order1688"
            maxlength="19"
            show-word-limit
          ></el-input>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input
            type="textarea"
            :rows="2"
            maxlength="1000"
            show-word-limit
            placeholder="选填，如果当前订单有更多快递或其他来源，请在此处填写说明"
            v-model.trim="currentForm.remark">
          </el-input>
        </el-form-item>
        <el-form-item label="包装服务">
          <sticker-package v-model="currentForm.packageType"></sticker-package>
        </el-form-item>
        <el-form-item label="本次代贴单费用">
          <div style="color: #ED6A0C;font-size: 16px">{{calStickerPrice}} 元</div>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogForm = false">取 消</el-button>
        <el-button type="primary" @click="submitForm" :loading="loading">确 定</el-button>
      </div>
    </el-dialog>

    <el-dialog
      class="auth-dialog"
      :visible.sync="dialogAuth"
      width="800px"
    >
      <div class="content" >
        <div class="img">
          <img src="@/assets/images/ali1688.png" alt="" >
          <div class="auth-img-jump" @click="jumpAuth"></div>
        </div>
        <div>
          <p>您尚未授权我们的系统「{{getAppName()}}」访问您1688订单的物流信息，因此该功能暂无法使用</p>
          <p>授权后，系统才能加载必要的数据信息提供完整的功能服务</p>
        </div>
        <div style="display: flex;align-items: center;justify-content: center;" class="mgt10">
          <el-button type="1688" @click="jumpAuth">前往授权</el-button>
        </div>
      </div>
    </el-dialog>

  </div>
</template>

<script>
import {deliveryOrder, getExpressList, order1688} from "@/api/order";
import {deepCopy} from "@/utils";
import {EventBus} from "@/utils/eventBus";
import {getAppName} from "@/utils/settings";
import StickerPackage from "@/views/order/components/stickerPackage";

export default {
  name: "Ali1688Button",
  components: {StickerPackage},
  props:{
    orderId:{
      type: String,
    },
    disable:{
      default: false,
    },
    orderArr:{
      type: Array,
      require: true,
    }
  },
  computed:{
    // 计算总的贴单金额
    calStickerPrice(){
      let sumPrice = 0;
      if(this.stickerType === 1){
        let sumStickerPrice = this.$cal.accMul(this.orderArr.length,this.stickerPrice);
        sumPrice = this.$cal.accAdd(sumPrice,sumStickerPrice)
      }else{
        this.orderArr.forEach(item=>{
          switch (item.siteId){
            case 'MLM': // 墨西哥
              sumPrice = this.$cal.accAdd(sumPrice,this.MLMPrice)
              break;
            case 'MLB':
              sumPrice = this.$cal.accAdd(sumPrice,this.MLBPrice)
              break;
            case 'MLC':
              sumPrice = this.$cal.accAdd(sumPrice,this.MLCPrice)
              break;
            case 'MCO':
              sumPrice = this.$cal.accAdd(sumPrice,this.MCOPrice)
              break;
          }
        })
      }

      const orderNum = this.orderArr.length;
      let packagePrice = 0; //包装服务费
      switch (this.currentForm.packageType){
        case 'vacuum': // 真空
          packagePrice = this.$cal.accMul(orderNum,this.vacuum)
          break;
        case 'leatheroid': // 纸皮
          packagePrice = this.$cal.accMul(orderNum,this.leatheroid)
          break;
        case 'special': // 特殊
          packagePrice = this.$cal.accMul(orderNum,this.special)
          break;
      }

      sumPrice = this.$cal.accAdd(sumPrice,packagePrice)

      return sumPrice;
    }
  },
  created() {
    // console.log(this.expressGroupOptions);
    // this.getOptions()
    this.setStoreParams();
  },
  data(){
    return {
      dialogForm: false,
      dialogAuth: false, // 授权dialog
      loading: false,
      currentForm:{
        orderIds: [], // 订单号
        order1688: '', // 1688订单号
        remark: '', // 备注
        packageType: '', // 选择包装类型
      },
      rules:{
        order1688:[
          { required: true, message: '请填写1688订单号', trigger: 'blur' },
          { pattern: /^[0-9]+$/, message: '1688订单号格式有误'},
        ],
      },
      stickerType: 1,
      stickerPrice: 0,
      vacuum: 0,
      leatheroid: 0,
      special: 0,
      MLMPrice: 0, // 墨西哥
      MLBPrice: 0, // 巴西
      MLCPrice: 0, // 智利
      MCOPrice: 0, // 哥伦比亚
    };
  },
  methods:{
    getAppName,
    getOptions(){
      getExpressList()
        .then(res=>{
          this.options = res.data
        })
    },
    use1688(){
      if(this.$store.getters.auth1688){
        EventBus.$emit('show1688AuthDialog');
        return;
      }else{
        this.dialogForm = true;
      }
    },
    jumpAuth(){
      window.open(this.$store.getters.auth1688Url,'_blank')
    },
    submitForm(){
      this.$refs.currentForm.validate(valid =>{
        if(valid){
          this.loading = true
          this.currentForm.orderIds = []
          this.orderArr.forEach(item=>{
            this.currentForm.orderIds.push(item.orderId)
          })
          const postData = deepCopy(this.currentForm)
          order1688(postData)
            .then(res=>{
              this.loading = false
              if(res.error === 0){
                this.$successMessage(res.message)
                this.dialogForm = false;
                // 通知父组件 订单状态发生了改变
                this.$emit('changeOrder',true)
              }else if(res.error === 300){
                EventBus.$emit('showRecharge',res.message);
              }else{
                this.$errorMessage(res.message)
              }
            })
        }
      })
    },
    // 设置贴单价格参数
    setStoreParams(){
      this.stickerType = this.$store.getters.stickerType;
      this.stickerPrice = this.$store.getters.stickerPrice;
      this.vacuum = this.$store.getters.vacuum;
      this.leatheroid = this.$store.getters.leatheroid;
      this.special = this.$store.getters.special;
      this.MLMPrice = this.$store.getters.MLMPrice;
      this.MLBPrice = this.$store.getters.MLBPrice;
      this.MLCPrice = this.$store.getters.MLCPrice;
      this.MCOPrice = this.$store.getters.MCOPrice;
    },
  }
}
</script>

<style scoped lang="scss">
.auth-dialog{
  .content{
    margin: 0 auto;
    width: 600px;
    padding: 30px 0;
    .img{
      text-align: center;
    }
    p{
      margin: 0;
      padding: 0;
      line-height: 20px;
      font-weight: 650;
      color: #1E3369;
      font-size: 13px;
      text-align: center;
    }
  }
  .auth-img-jump{
    position: absolute;
    cursor: pointer;
    //background: #cccccc;
    width: 150px;
    height: 150px;
    top: 180px;
    left: 320px;
  }
}
.order1688-tips{
  position: absolute;
  top: 50px;
  left: 21px;
  color: #aaaaaa;
}
.header-title{
  font-size: 18px;
}
</style>
